package rsj.api.org;

import com.rlyy.aibase.utils.base.Box;
import com.rlyy.aibase.utils.base.MapNode;
import com.rlyy.aibase.utils.base.TableNode;
import com.rlyy.aibase.utils.base.ThinkException;
import common.util.Assert;
import common.util.BoxUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import rsj.api.account.entity.AuthInfo;
import rsj.service.auth.IAuthService;
import rsj.util.PageQuery;
import rsj.util.PaginationData;

import java.util.List;

@RestController
public class GetAuthInfo {

    //private static AuthService authService = AuthService.getInstance();

    @Autowired
    private IAuthService authServiceImpl;

    @RequestMapping(value = "rsj_org_authInfo")
    public Box executeLink(Box in) throws ThinkException {
        MapNode data = in.getMapNode("data");
//        String orgId = data.getValue("Org_Id");
//        Assert.isNotBlank(orgId, "机构ID不能为空！");
        PageQuery pageQuery = createPageQuery(data);
        String searchName = data.getValue("Search_Name");
        PaginationData<TableNode> paginationData = authServiceImpl.authInfoList(pageQuery,searchName);
        MapNode out = new MapNode();
        out.putNode("count", String.valueOf(paginationData.getTotalCount()));
        out.putNode("list", paginationData.getDataList().toJson());
        return BoxUtil.successBoxToData(in, "获取列表信息！", out);
    }



    /**
     * 查询客户下的账号
     *
     * @param param 参数
     * @return {@link Box}
     */
    @RequestMapping(value = "rsj_auth_list_customer")
    public Box authListByCustomer(Box param) {
        String custId = param.getMapNode("data").getValue("cust_id");
        Assert.isNotBlank(custId, "客户id不可为空");
        List<AuthInfo> authInfos = authServiceImpl.authListByCustomer(custId);
        return BoxUtil.successBoxToTableData(param, authInfos);
    }



    private PageQuery createPageQuery(MapNode data) {
        PageQuery query = new PageQuery();
        if(data == null || data.size() == 0){
           return  query;
        }
        query.setPage_Count(data.getValueIntDef("Page_Count", 0));
        query.setPage_Size(data.getValueIntDef("Page_Size", 0));
        return query;
    }

}
